package com.meichis.yndms.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.InputType;
import android.text.TextUtils;
import android.widget.CompoundButton;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.meichis.mcsappframework.gesturelockview.CustomLockView;
import com.meichis.mcsappframework.utils.MessageTools;
import com.meichis.mcsappframework.utils.SharePreferenceUtil;
import com.meichis.yndms.R;
import com.meichis.yndms.common.MCode;
import com.meichis.yndms.databinding.ActivityGestureLockBinding;
import com.meichis.yndms.ui.common.BaseActivity;
import com.meichis.yndms.vm.GestureLockVM;

public class GestureLockActivity extends BaseActivity {

    private ActivityGestureLockBinding binding;
    private GestureLockVM vm;
    private CustomLockView.LockMode mode;

    public static Intent getStateIntent(Context context, CustomLockView.LockMode mode) {
        Intent intent = new Intent(context, GestureLockActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(MCode.ENUM, mode);
        intent.putExtras(bundle);
        return intent;
    }


    @Override
    protected void setDataBindingContentView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_gesture_lock);
        vm =new ViewModelProvider(this, new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(GestureLockVM.class);
        binding.setVm(vm);
        binding.setClick(new ClickEvent());
        binding.setLifecycleOwner(this);
    }

    @Override
    protected void init() {
        mode = (CustomLockView.LockMode) getIntent().getExtras().getSerializable(MCode.ENUM);
        if (mode == null) {
            mode = CustomLockView.LockMode.SETTING_PASSWORD;
        }
        switch (mode) {
            case SETTING_PASSWORD:
                binding.navigation.txtTitle.setText("设置手势图案");
                binding.tvHint.setText("绘制解锁图案");
                break;
            case EDIT_PASSWORD:
                binding.navigation.txtTitle.setText("修改手势图案");
                binding.tvHint.setText("请输入原手势密码");
                break;
            case CLEAR_PASSWORD:
                binding.navigation.txtTitle.setText("验证手势图案");
                binding.tvHint.setText("请输入原手势密码");
                break;
            default:
        }
        binding.lvLock.setMode((CustomLockView.LockMode) getIntent().getExtras().getSerializable(MCode.ENUM));
        binding.navigation.navBack.setOnClickListener(v -> onBackPressed());

        if (!TextUtils.isEmpty(util.getStringValue(SharePreferenceUtil.PREFERENCES_PASSWORD))) {
            binding.vfViewFlipper.setDisplayedChild(1);
        } else {
            binding.tvHint.setText("请输入账号验证");
        }
        binding.lvLock.setPasswordMinLength(4);
        binding.lvLock.setOnCompleteListener(onCompleteListener);

        binding.cbShowPwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked) {
                    binding.userPwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    binding.userPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
            }
        });

        addObserver();
    }

    private void addObserver() {
        vm.getShowDialog().observe(this, aBoolean -> showLoading(aBoolean));

        vm.getDspToken().observe(this, dspToken -> {
            binding.vfViewFlipper.setDisplayedChild(1);
            binding.vfViewFlipper.setInAnimation(this, R.anim.vf_right_in);
            binding.vfViewFlipper.setOutAnimation(this, R.anim.vf_right_out);
            binding.lvLock.setMode(CustomLockView.LockMode.SETTING_PASSWORD);
            binding.tvHint.setText("绘制解锁图案");
            showMsg("请重新绘制解锁图案");
            binding.lvLock.clearCurrent();
        });
    }


    private CustomLockView.OnCompleteListener onCompleteListener = new CustomLockView.OnCompleteListener() {
        @Override
        public void onComplete(CustomLockView.LockMode mode) {
            switch (mode) {
                case SETTING_PASSWORD://设置密码
                case EDIT_PASSWORD://修改密码
                    binding.tvHint.setText("您的新解锁图案");
                    showMsg("绘制成功");
                    close();
                    break;
                case VERIFY_PASSWORD:
//                    new DropDownDialog<>(GestureLockActivity.this, R.color.red, "请选择", "", modeList, new DropDownDialog.SelectedListener() {
//                        @Override
//                        public void setSelect(int position) {
//                            if (position == 0) {
                    //                               lvLock.setMode(CustomLockView.LockMode.SETTING_PASSWORD);
//                                tvHint.setText("绘制解锁图案");
//                                lvLock.clearCurrent();
//                            } else {
//                                lvLock.clearPassWord();
//                                showMsg("已关闭");
//                                onBackPressed();
//                            }
//                        }
//
//                    }).show();
                    break;
                case CLEAR_PASSWORD://清除密码:
                    binding.lvLock.clearPassWord();
                    showMsg("已清除");
                    onBackPressed();
                    break;
                default:
            }
        }

        @Override
        public void onError(String errorTimes) {
            binding.tvHint.setText("绘制错误，请重新绘制");
        }

        @Override
        public void onPasswordIsShort(int passwordMinLength) {
            binding.tvHint.setText("至少需连接" + passwordMinLength + "个点，请重试");
        }

        @Override
        public void onAginInputPassword(CustomLockView.LockMode mode, String password, int[] indexs) {
            binding.tvHint.setText("请再次绘制图案");
            binding.lvLock.clearCurrent();
        }

        @Override
        public void onInputNewPassword() {
            binding.tvHint.setText("绘制解锁图案");
            binding.lvLock.clearCurrent();
        }

        @Override
        public void onEnteredPasswordsDiffer() {
            binding.tvHint.setText("两次绘制不一致");
            binding.lvLock.clearCurrent();
        }

        @Override
        public void onErrorNumberMany() {
            binding.tvHint.setText("错误次数超过限制，请稍后再试");
            CountDownTimer timer = new CountDownTimer(60000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    binding.tvHint.setText("错误次数超过限制，请" + millisUntilFinished / 1000 + "秒后再试");
                }

                @Override
                public void onFinish() {
                    binding.tvHint.setText("绘制您的解锁图案");
                    binding.lvLock.clearCurrent();
                }
            };
            timer.start();// 开始计时　
        }
    };


    public class ClickEvent {

        public void onCancel() {
            binding.lvLock.clearPassWord();
            onBackPressed();
        }

        public void onToggle() {
            binding.tvHint.setText("请输入账号验证");
            binding.vfViewFlipper.setDisplayedChild(0);
            MessageTools.ShowmessageDialog(GestureLockActivity.this, "使用账号登录之后，即可重新设定手势密码");
        }

        public void onLogin() {
            if (TextUtils.isEmpty(binding.userName.getText().toString()) || TextUtils.isEmpty(binding.userPwd.getText().toString())) {
                showMsg("请先输入登录账号");
                return;
            }
            vm.Login();
        }
    }


}
